package com.cqs.leetcode.array;

import com.cqs.leetcode.ds.ArraysUtils;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

class RandomFlipMatrix519 {
    private int M;
    private int N;
    private int total;

    Set<Integer> used = new HashSet<>();

    private Random r = new Random();

    public RandomFlipMatrix519(int m, int n) {
        this.M = m;
        this.N = n;
        total = M * N;
    }

    public int[] flip() {
        int i = 0, j = 0;
        int val = 0;

        do {
            val = r.nextInt(total);
            i = val / N;
            j = val % N;
            //高16位存i 低16存j
           // val = i << 16 + j;
        } while (used.contains(val));
        used.add(val);
        return new int[]{i, j};
    }

    public void reset() {
        used.clear();
    }

    public static class Solution {
        private int[][] source;
        private Random r = new Random();
        private Random r2 = new Random();
        private int M;
        private int N;

        public Solution(int m, int n) {
            this.M = m;
            this.N = n;
            source = new int[m][n];
        }

        public int[] flip() {
            int i = 0, j = 0;
            do {
                i = r.nextInt(M);
                j = r2.nextInt(N);
            } while (source[i][j] != 0);
            source[i][j] = 1;
            return new int[]{i, j};
        }

        public void reset() {
            for (int i = 0; i < M; i++) {
                for (int j = 0; j < N; j++) {
                    source[i][j] = 0;
                }
            }
        }
    }


    public static void main(String args[]) {
        RandomFlipMatrix519 s = new RandomFlipMatrix519(10000, 10000);

        for (int i = 0; i < s.M; i++) {
            for (int j = 0; j < s.N; j++) {
                System.out.println(" i =  " + i + " j = " + j + " result = " + ArraysUtils.formatInts(s.flip()));
            }
        }
        System.out.println(s.used.size());
    }
}